#include "options.h"
#include <QtDeclarative>

QNMOptions::QNMOptions(QNMSettings * qset, QObject *_parent) : QObject(_parent)
{
    qset_this = qset;
    parent = _parent;
    myOptIfaces = NULL;

    listFormatDT.append("yyyy-MM-dd HH:mm:ss");
    listFormatDT.append("MM-dd-yyyy HH:mm:ss");
    listFormatDT.append("dd-MM-yyyy HH:mm:ss");
    listFormatDT.append("dd.MM.yyyy HH:mm:ss");
    listFormatDT.append("MM.dd.yyyy HH:mm:ss");
    listFormatDT.append("yyyy.MM.dd HH:mm:ss");
    listFormatDT.append("dd/MM/yyyy HH:mm:ss");
    listFormatDT.append("MM/dd/yyyy HH:mm:ss");
    listFormatDT.append("yyyy/mm/dd HH:mm:ss");
}

QNMOptions::~QNMOptions()
{
    if( myOptIfaces ) {
        delete myOptIfaces;
    }
}

void QNMOptions::initSettings()
{
    QString formatDT = qset_this->getDataTimeFormat();
    QString id_formatDT = "datetimeformat" + QString::number( listFormatDT.indexOf( formatDT ) );

    QString lang = qset_this->getLanguage();
    QString id_lang = "lang_"+ lang;

    QDeclarativeView *qmlObj = (QDeclarativeView*)(this->parent);
    QObject* pageSettings = (qmlObj->rootObject())->findChild<QObject*>( QString("pageSettings") );

    if( pageSettings ) {
        /* Формат дата-время*/
        /*
        char * pr =  id_formatDT.toAscii().data();
        QVariant value = pageSettings->property( pr );
        QMetaObject::invokeMethod( pageSettings, "setButtonTextDataTimeFormat", Q_ARG(QVariant,value) );
        */

        /* Язык интерфейса */
        char *pr =  id_lang.toAscii().data();
        QVariant value = pageSettings->property( pr );
        QMetaObject::invokeMethod( pageSettings, "setButtonTextLanguage", Q_ARG(QVariant,value) );
    }

    /* тема: светлая и темная */
    QString theme = qset_this->getTheme();
    qmlObj->engine()->rootContext()->setContextProperty( "themeAppl", theme );
}

void QNMOptions::changeFormatDT(const int indx_changed)
{
    QString frmt = listFormatDT.at(indx_changed);
    qset_this->setDataTimeFormat( frmt );
}

void QNMOptions::changeLanguage( const int indx_changed )
{
    if( indx_changed == 0 ) {
        qset_this->setLanguage( QString("0") ); //auto
    } else if(indx_changed == 1) {
        qset_this->setLanguage( QString("en") ); //en
    } else if(indx_changed == 2) {
        qset_this->setLanguage( QString("ru") ); //ru
    } else if(indx_changed == 3) {
        qset_this->setLanguage( QString("hu") ); //hu
    }
}

void QNMOptions::changeTheme(const QString &theme)
{
    if( (theme == "dark") || (theme == "light") )
    {
        qset_this->setTheme( theme );
    }
}

void QNMOptions::changeOrientation(const QString &orientation)
{
    QDeclarativeView *qmlObj = (QDeclarativeView*)(this->parent);

    if( (orientation == "landscape") || (orientation == "portrait") || (orientation == "auto") )
    {
        qset_this->setOrientation( orientation );
        qmlObj->engine()->rootContext()->setContextProperty( "orientationAppl", orientation );
    } else {
        QString o("auto");
        qset_this->setOrientation( o );
        qmlObj->engine()->rootContext()->setContextProperty( "orientationAppl", o );
    }

}

/*  вызывается из QML формы для инициализации окна с добавлением/удалением сетевых интерфейсов */
void QNMOptions::initOptionsIfaces()
{
    if( myOptIfaces == NULL ) {
        myOptIfaces = new OptionsIfaces(qset_this, this->parent);

        QDeclarativeView *qmlObj = (QDeclarativeView*)(this->parent);
        QDeclarativeEngine *engine = qmlObj->engine();
        engine->rootContext()->setContextProperty( "cppOptIfces", myOptIfaces );
    }
    myOptIfaces->initListIfaces();
    myOptIfaces->initIfconfig();
}
